//判断是否支持webp
const supportsWebp = ({ createImageBitmap, Image }: any) => {
  if (!createImageBitmap || !Image) return Promise.resolve(false);

  return new Promise((resolve) => {
    let tWebp: any = window.localStorage.getItem('webp');
    if (!tWebp) {
      const image = new Image();
      image.onload = () => {
        createImageBitmap(image)
          .then(() => {
            resolve(true);
            window.localStorage.setItem('webp', '1');
          })
          .catch(() => {
            resolve(false);
            window.localStorage.setItem('webp', '0');
          });
      };
      image.onerror = () => {
        resolve(false);
      };
      image.src =
        '';
    } else {
      if (tWebp == 1) {
        resolve(true);
      } else {
        resolve(false);
      }
    }
  });
};

export default {
  supportsWebp,
};
